const express = require("express")
const router = express.Router()
const pool = require("../../db/db")
const Response = require("../../utils/ResponseData")
/**
 * 1. 首页查询`最新三条日志`接口
 * 请求url：http://localhost:3001/daily/getThree
 */
router.get('/getThree', (req, res, next) => {
    // 编写sql(越是最新插入的数据，id越大)
    // 按照id排序，取最新三条
    const sql = "select * from pet_diary order by d_id desc limit 0,3"
    // 执行sql
    pool.query(sql, (err, result) => {
        if (err) {
            console.log("Daily.js=>/getThree接口：", err)
            next(err)
            return
        }
        if (result.length > 0) {
            res.send(Response.success(result, "日志查询成功！"))
        } else {
            res.send(Response.error("日志查询失败！"))
        }
    })
})
/**
 * 2. 查询`全部日志`接口
 * 请求url：http://localhost:3001/daily/getAll/:page/:pageSize
 * @param page:当前页码
 * @param pageSize:每页显示条数
 */
router.get('/getAll/:page/:pageSize', (req, res, next) => {
    // 获取请求的参数
    let { page, pageSize } = req.params
    // 编写sql
    const sql = "select * from pet_diary order by d_id desc limit ?,?"
    // 计算startIndex
    let startIndex = (page - 1) * pageSize
    // 将字符串转换为数值
    pageSize = parseInt(pageSize)
    // 执行sql
    pool.querySync(sql, [startIndex, pageSize])
        .then(result => {
            if (result.length > 0) {
                res.send(Response.success(result, "日志查询成功！"))
            } else {
                res.send(Response.error("日志查询失败！"))
            }
        })
        .catch(err => {
            if (err) {
                console.log("Daily.js=>/getAll接口：", err)
                next(err)
                return
            }
        })
})
/**
 * 2. 查询`全部日志`接口
 * 请求url：http://localhost:3001/daily/getAll
 * @param page:当前页码
 * @param pageSize:每页显示条数
 */
router.get('/getAll', (req, res, next) => {
    // 获取请求的参数
    let { page, pageSize } = req.params
    // 编写sql
    const sql = "select * from pet_diary order by d_id desc"
    // 执行sql
    pool.querySync(sql)
        .then(result => {
            if (result.length > 0) {
                res.send(Response.success(result, "日志查询成功！"))
            } else {
                res.send(Response.error("日志查询失败！"))
            }
        })
        .catch(err => {
            if (err) {
                console.log("Daily.js=>/getAll接口：", err)
                next(err)
                return
            }
        })
})
/**
 * 3. 通过日志id查询日志接口
 * 请求url：http://localhost:3001/daily/getById/:id
 * @param id:日志编号
 */
router.get('/getById/:id', (req, res, next) => {
    let { id } = req.params;
    // 编写sql(越是最新插入的数据，id越大)
    // 按照id排序，取最新三条
    const sql = "select * from pet_diary where d_id=?"
    // 执行sql
    pool.query(sql, [id], (err, result) => {
        if (err) {
            console.log("Daily.js=>/getById接口：", err)
            next(err)
            return
        }
        if (result.length > 0) {
            res.send(Response.success(result, "日志查询成功！"))
        } else {
            res.send(Response.error("日志查询失败！"))
        }
    })
})
module.exports = router